package com.oromwin.repos.http.handler;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

public class ExitHandler implements HttpHandler {
    private static Log log = Logger.getLog(ExitHandler.class);
    private static boolean isRunning = true;

    @Override
    public void handle(HttpExchange h) throws IOException {
        try {
            String response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml>OK</xml>";
            byte[] responseBytes = response.getBytes("utf-8");
            h.getResponseHeaders().set("Content-Type", "text/plain"); // http://en.wikipedia.org/wiki/Internet_media_type
            h.sendResponseHeaders(200, responseBytes.length);
            OutputStream os = h.getResponseBody();
            os.write(responseBytes);
            os.close();
            isRunning = false;
            if (log.isInfoEnabled()) log.info("Exit Request has received.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isRunning() {
        return isRunning;
    }
}
